package cn.tqfeiyang.smartadmin.system.service;

import cn.tqfeiyang.smartadmin.commons.pagination.QueryParams;
import cn.tqfeiyang.smartadmin.commons.toolbox.CollectionUtils;
import cn.tqfeiyang.smartadmin.system.entity.PermissionEntity;
import cn.tqfeiyang.smartadmin.system.entity.RoleEntity;
import cn.tqfeiyang.smartadmin.system.mapper.PermissionMapper;
import cn.tqfeiyang.smartadmin.system.mapper.RolePermissionMapper;
import cn.tqfeiyang.smartadmin.system.request.PermissionCreateDTO;
import cn.tqfeiyang.smartadmin.system.request.PermissionUpdateDTO;
import cn.tqfeiyang.smartadmin.system.request.PermissionQueryDTO;
import cn.tqfeiyang.smartadmin.system.response.PermissionQueryVO;
import cn.tqfeiyang.smartadmin.system.response.PermissionDetailVO;

import cn.tqfeiyang.smartadmin.system.response.RoleQueryVO;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import cn.tqfeiyang.smartadmin.commons.exception.BusinessException;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.toolbox.BeanUtils;
import cn.tqfeiyang.smartadmin.commons.toolbox.ValidationUtils;

import java.util.List;
import java.util.Set;

/**
 * 权限服务
 *
 * @author tqfeiyang
 * @since 2024-08-23
 */
@Service
public class PermissionService {

    /**
     * 权限映射接口
     */
    @Resource
    private PermissionMapper permissionMapper;

    /**
     * 角色-权限映射接口
     */
    @Resource
    private RolePermissionMapper rolePermissionMapper;

    /**
     * 查询权限
     *
     * @param dto 请求数据
     * @return 权限列表
     */
    public PageData<PermissionQueryVO> queryPermissions(PermissionQueryDTO dto) {
        ValidationUtils.validateProperties(dto);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<PermissionEntity> list = permissionMapper.selectList(dto.getWrapper());
        return PageData.create(list, PermissionQueryVO.class);
    }

    /**
     * 获取权限
     *
     * @param permissionId 权限id
     * @return 权限信息
     */
    public PermissionDetailVO readPermission(Long permissionId) {
        PermissionEntity permissionEntity = checkPermissionExists(permissionId);
        return BeanUtils.map(permissionEntity, PermissionDetailVO.class);
    }

    /**
     * 添加权限
     *
     * @param dto 请求数据
     * @return 权限id
     */
    @Transactional(rollbackFor = Exception.class)
    public Long createPermission(PermissionCreateDTO dto) {
        ValidationUtils.validateProperties(dto);
        PermissionEntity permissionEntity = BeanUtils.copy(dto, PermissionEntity.class);
        permissionMapper.insert(permissionEntity);
        return permissionEntity.getPermissionId();
    }

    /**
     * 修改权限
     *
     * @param permissionId 权限id
     * @param dto 请求数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void updatePermission(Long permissionId, PermissionUpdateDTO dto) {
        checkPermissionExists(permissionId);
        ValidationUtils.validateProperties(dto);
        PermissionEntity permissionEntity = BeanUtils.copy(dto, PermissionEntity.class);
        permissionEntity.setPermissionId(permissionId);
        permissionMapper.updateById(permissionEntity);
    }

    /**
     * 删除权限
     *
     * @param permissionId 权限id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deletePermission(Long permissionId) {
        checkPermissionExists(permissionId);
        permissionMapper.deleteById(permissionId);
        rolePermissionMapper.deleteRoleIdsByPermissionId(permissionId);
    }

    /**
     * 检查权限实体是否存在
     *
     * @param permissionId 权限id
     * @return 权限实体
     */
    protected PermissionEntity checkPermissionExists(Long permissionId) {
        Assert.notNull(permissionId, "permissionId不能为空");
        PermissionEntity permissionEntity = permissionMapper.selectById(permissionId);
        if (ObjectUtils.isEmpty(permissionEntity)) {
            throw new BusinessException("权限不存在");
        }
        return permissionEntity;
    }

    /**
     * 查询角色
     *
     * @param permissionId 菜单id
     * @param dto 请求数据
     * @return 角色列表
     */
    public PageData<RoleQueryVO> queryRoles(Long permissionId, QueryParams dto) {
        checkPermissionExists(permissionId);
        ValidationUtils.validateProperties(dto);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<RoleEntity> list = rolePermissionMapper.selectRoleListByPermissionId(permissionId);
        return PageData.create(list, RoleQueryVO.class);
    }

    /**
     * 更新角色
     *
     * @param permissionId 菜单id
     * @param roleIds 角色id集合
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateRoles(Long permissionId, Set<Long> roleIds) {
        checkPermissionExists(permissionId);
        rolePermissionMapper.deleteRoleIdsByPermissionId(permissionId);
        if (CollectionUtils.isNotEmpty(roleIds)) {
            rolePermissionMapper.insertRoleIdsByPermissiond(permissionId, roleIds);
        }
    }

}
